/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 2.1 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

package org.systemsbiology.util;

import java.util.Stack;

/**
 * Trivial extension of {@link Stack} to allow for a cleaner printable representation
 * @author trobinso
 */
public class PrintableStack<T> extends Stack<T> {

    private String token = ".";     /** Token value used to denote item splits when printing this stack */

    /**
     * Default constructor, specifying a token of "."
     */
    public PrintableStack() {}

    /**
     * Long constructor, allowing for specification of the token value used when printing all terms on the stack
     * @param token {@link String}Token value used to denote item splits when printing this stack
     */
    public PrintableStack(String token) {
        assert(token != null);

        this.token = token;
    }

    /**
     * Overridden {@code toString} method, pretty-printing the output
     * @return {@link String} representation of the current stack, bottom to top, stapled together by our given token
     */
    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Object o: this.toArray()) {
            s.append(o.toString());
            s.append(this.token);
        }
        if (s.length() == 0) return "";

        s.delete(s.length() - token.length(), s.length());
        return s.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        PrintableStack that = (PrintableStack) o;

        if (token != null ? !token.equals(that.token) : that.token != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (token != null ? token.hashCode() : 0);
        return result;
    }
}
